<?php
// 开启会话
session_start();

// 初始化游戏数据
if (!isset($_SESSION['game'])) {
    $_SESSION['game'] = [
        'current_level' => 1,
        'player_x' => 0,
        'player_health' => 100,
        'levels' => [
            1 => [
                'length' => 10,
                'obstacles' => [3, 7],
                'enemies' => [5],
                'enemy_health' => [20]
            ],
            2 => [
                'length' => 15,
                'obstacles' => [2, 6, 10],
                'enemies' => [4, 8, 12],
                'enemy_health' => [30, 30, 30]
            ]
        ]
    ];
}

// 处理玩家操作
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
    $action = $_POST['action'];
    $game = &$_SESSION['game'];
    $current_level = $game['current_level'];
    $level = $game['levels'][$current_level];

    switch ($action) {
        case 'move_right':
            if ($game['player_x'] < $level['length'] - 1) {
                $game['player_x']++;
                if (in_array($game['player_x'], $level['obstacles'])) {
                    $game['player_health'] -= 10;
                    $message = "你撞到了障碍物，生命值减少 10 点！当前生命值: {$game['player_health']}";
                } elseif (in_array($game['player_x'], $level['enemies'])) {
                    $enemy_index = array_search($game['player_x'], $level['enemies']);
                    $level['enemy_health'][$enemy_index] -= 20;
                    if ($level['enemy_health'][$enemy_index] <= 0) {
                        unset($level['enemies'][$enemy_index]);
                        unset($level['enemy_health'][$enemy_index]);
                        $level['enemies'] = array_values($level['enemies']);
                        $level['enemy_health'] = array_values($level['enemy_health']);
                        $message = "你击败了敌人！";
                    } else {
                        $game['player_health'] -= 10;
                        $message = "你与敌人战斗，生命值减少 10 点！当前生命值: {$game['player_health']}";
                    }
                }
            }
            break;
        case 'move_left':
            if ($game['player_x'] > 0) {
                $game['player_x']--;
            }
            break;
    }

    // 检查是否通过关卡
    if ($game['player_x'] == $level['length'] - 1 && empty($level['enemies'])) {
        if ($current_level < count($game['levels'])) {
            $game['current_level']++;
            $game['player_x'] = 0;
            $message = "你成功通过了第 {$current_level} 关，进入第 {$game['current_level']} 关！";
        } else {
            $message = "你成功通关了所有关卡，游戏胜利！";
        }
    }

    // 检查是否失败
    if ($game['player_health'] <= 0) {
        $message = "你的生命值归零，游戏失败！";
        // 重置游戏
        session_destroy();
        session_start();
        $_SESSION['game'] = null;
    }

    $game['levels'][$current_level] = $level;
}
?>

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>横板闯关网页游戏</title>
    <style>
        body {
            font-family: "Microsoft YaHei", sans-serif;
            background-color: #f4f4f4;
            padding: 20px;
        }

        h1 {
            text-align: center;
            color: #333;
        }

        p {
            margin-bottom: 10px;
        }

        form {
            background-color: #fff;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
            margin-bottom: 20px;
        }

        button {
            padding: 10px 20px;
            background-color: #007BFF;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            margin-right: 10px;
        }

        button:hover {
            background-color: #0056b3;
        }

        .message {
            padding: 10px;
            margin-bottom: 10px;
            border-radius: 5px;
        }

        .success {
            background-color: #d4edda;
            color: #155724;
        }

        .error {
            background-color: #f8d7da;
            color: #721c24;
        }
    </style>
</head>

<body>
    <h1>横板闯关网页游戏</h1>
    <?php if (isset($_SESSION['game'])): ?>
        <p>当前关卡: <?php echo $_SESSION['game']['current_level']; ?></p>
        <p>角色位置: <?php echo $_SESSION['game']['player_x']; ?></p>
        <p>生命值: <?php echo $_SESSION['game']['player_health']; ?></p>

        <?php if (isset($message)): ?>
            <div class="message <?php echo strpos($message, '失败')!== false? 'error' : 'success'; ?>">
                <?php echo $message; ?>
            </div>
        <?php endif; ?>

        <form method="post">
            <button type="submit" name="action" value="move_left">向左移动</button>
            <button type="submit" name="action" value="move_right">向右移动</button>
        </form>
    <?php else: ?>
        <p>游戏结束，你可以刷新页面重新开始。</p>
    <?php endif; ?>
</body>

</html>